package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IComplainFinder;
import com.gears_software.sxdc.dccx.data.finder.IZPOrderFinder;
import com.gears_software.sxdc.dccx.data.finder.RequestUrl;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.CPOrderDetailModel;
import com.gears_software.sxdc.dccx.data.model.MyTripPassengerDetailModel;
import com.gears_software.sxdc.dccx.data.model.MyTripZCDetailModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.request.RComplainModel;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/8/2.
 */
@EActivity(R.layout.activity_zc_complain)
public class ZCComplainActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView txt_nickname;
    @ViewById
    TextView tv_chepaihao;
    @ViewById
    TextView tv_chexing;
    @ViewById
    TextView tv_color;
    @ViewById
    EditText edit_complain;
    @ViewById
    SimpleDraweeView img_avatar;
    @Extra
    MyTripZCDetailModel zcDetailModel;
    @RestService
    IZPOrderFinder izpOrderFinder;
    @Bean
    GearsErrorHandler errorHandler;
    String flag = "2"; //2为乘客投诉司机


    @AfterViews
    void created() {
        actionbar_title.setText("投诉");
        initIconfont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        izpOrderFinder.setRestErrorHandler(errorHandler);
        if (zcDetailModel != null) {
            if (zcDetailModel.getAvatar() != null && !zcDetailModel.getAvatar().equals("")) {
                Uri uri = Uri.parse(zcDetailModel.getAvatar() + RequestUrl.avatar);
                img_avatar.setImageURI(uri);
            }
            if (zcDetailModel.getDrivername() != null && !zcDetailModel.getDrivername().equals("")) {
                txt_nickname.setText(zcDetailModel.getDrivername());
            } else {
                txt_nickname.setVisibility(View.GONE);
            }
            tv_chepaihao.setText(zcDetailModel.getVehicleno());
            tv_chexing.setText(zcDetailModel.getCarmodel());
            tv_color.setText(zcDetailModel.getVehiclecolor());
        }

    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setVisibility(View.GONE);
    }

    @Click
    void btn_submit() {
        if (edit_complain.getText().toString().equals("")) {
            TastyToast.makeText(this, "请填写投诉信息", app.SHORT, app.WARNING);
        } else {
            RComplainModel complainModel = new RComplainModel();
            complainModel.setId(zcDetailModel.getId());
            complainModel.setDetail(edit_complain.getText().toString().trim());
            complainModel.setFlag(flag);
            submitComplainInfo(complainModel);
        }
    }

    @Background
    void submitComplainInfo(RComplainModel complainModel) {
        ResultModel<String> res = izpOrderFinder.goZCComplaint(complainModel);
        showResult(res);
    }

    @UiThread
    void showResult(ResultModel<String> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                TastyToast.makeText(this, "投诉信息提交成功", app.SHORT, app.SUCCESS);
                finish();
            } else {
                TastyToast.makeText(this, "投诉信息提交失败", app.SHORT, app.ERROR);
            }
        }

    }

    @Click
    void back_icon() {
        finish();
    }
}
